﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cms.Tests.Unit.Domain
{

    [TestClass]
    public class MediaFolderTests
    {

        [TestMethod]
        public void IsParentTo()
        {
            MediaFolder a = new MediaFolder();
            MediaFolder b = new MediaFolder();
            MediaFolder c = new MediaFolder();
            b.Parent = a;
            c.Parent = b;
            Assert.IsTrue(a.IsParentTo(c));
            Assert.IsTrue(b.IsParentTo(c));
            Assert.IsFalse(c.IsParentTo(a));
        }


        [TestMethod]
        public void GetVirtualPath()
        {
            Site site = new Site { RootMediaFolder = "media" };
            MediaFolder a = new MediaFolder { Site = site, Name = "a" };
            MediaFolder b = new MediaFolder { Site = site, Name = "b", Parent = a };
            MediaFolder c = new MediaFolder { Site = site, Name = "c", Parent = b };
            Assert.AreEqual("media/a", a.GetVirtualPath());
            Assert.AreEqual("media/a/b", b.GetVirtualPath());
            Assert.AreEqual("media/a/b/c", c.GetVirtualPath());
        }


        [TestMethod]
        public void GetPhysicalPath()
        {
            Site site = new Site { RootMediaFolder = "media" };
            MediaFolder a = new MediaFolder { Site = site, Name = "a" };
            MediaFolder b = new MediaFolder { Site = site, Name = "b", Parent = a };
            MediaFolder c = new MediaFolder { Site = site, Name = "c", Parent = b };
            Assert.AreEqual(@"media\a\b\c", c.GetPhysicalPath());
        }


        [TestMethod]
        public void GetFile()
        {
            MediaFolder folder = new MediaFolder();
            MediaFile file = new MediaFile {Name = "SomeFile.pdf"};
            folder.Files.Add(file);
            Assert.AreSame(file, folder.GetFile("sOMEfILE.PDF")); // Casing shouldn't matter
        }


        [TestMethod]
        public void PrepareRecursiveRemoval_RemoveAllDependencies()
        {
            Site site = new Site { RootMediaFolder = "media" };
            MediaFolder a = new MediaFolder { Site = site, Name = "a" };
            site.MediaFolders.Add(a);
            MediaFolder b = new MediaFolder { Site = site, Name = "b", Parent = a };
            site.MediaFolders.Add(b);
            a.SubFolders.Add(b);
            MediaFolder c = new MediaFolder { Site = site, Name = "c", Parent = b };
            site.MediaFolders.Add(c);
            b.SubFolders.Add(c);
            MediaFile fileA = new MediaFile {Site = site, Folder = c};
            c.Files.Add(fileA);
            a.PrepareRecursiveRemoval();
            Assert.AreEqual(0, site.MediaFolders.Count);
            Assert.IsNull(a.Site);
            Assert.IsNull(b.Site);
            Assert.IsNull(c.Site);
            Assert.AreEqual(0, a.SubFolders.Count);
            Assert.AreEqual(0, b.SubFolders.Count);
            Assert.IsNull(b.Parent);
            Assert.IsNull(c.Parent);
            Assert.AreEqual(0, c.Files.Count);
            Assert.IsNull(fileA.Folder);
        }


        [TestMethod]
        public void GetAllFiles_TwoFilesInSubFolders_ReturnTwo()
        {
            MediaFolder a = new MediaFolder();
            MediaFolder b = new MediaFolder();
            MediaFolder c = new MediaFolder();
            a.SubFolders.Add(b);
            b.Parent = a;
            b.SubFolders.Add(c);
            c.Parent = b;
            MediaFile fileA = new MediaFile();
            MediaFile fileB = new MediaFile();
            b.Files.Add(fileA);
            fileA.Folder = b;
            c.Files.Add(fileB);
            fileB.Folder = c;
            Assert.AreEqual(2, a.GetAllFiles().Count);
        }


        [TestMethod]
        public void IsInCategory_GrandParentIsAssociated_Success()
        {
            MediaFolder a = new MediaFolder();
            MediaFolder b = new MediaFolder();
            MediaFolder c = new MediaFolder();
            a.Name = "a";
            b.Name = "b";
            c.Name = "c";
            b.Parent = a;
            c.Parent = b;
            Category category = new Category() { ID = 1, Name = "Services" };
            a.Categories.Add(category);
            category.MediaFolders.Add(a);
            Assert.IsTrue(c.IsInCategory("Services"));
        }

    }

}
